package FirstExam10tasks;

import java.util.Scanner;

public class Task10 {

	public static void main(String[] args) 
	{
		
		Scanner sc = new Scanner (System.in);
		System.out.println("Enter a word");
		String str = sc.nextLine();
		
		//String str = "ivan vari rakiya vsyaka sutrin";
		//String str = "the quick brown fox";

		StringBuilder sb = new StringBuilder();
		sb.append(str);
		
		System.out.println(sb);
		System.out.println();
		
		sb = doThePigLatin(sb);
		System.out.println(sb);
		
	}	
		
	public static StringBuilder doThePigLatin (StringBuilder sb)
	{
		int i = 0;
		int indHelp = 0;
		String help = sb.charAt(indHelp) + "ay";         //add "ay" to the first simbol
		
		sb.insert(sb.indexOf(" ", i), help);			//insert help on first free space
		sb.deleteCharAt(i);								//delete first char
		int itr = sb.indexOf(" ", sb.indexOf(" "));
		
		while(itr != sb.lastIndexOf(" "))
		{
			indHelp = sb.indexOf(" ",indHelp) + 1;			//find the index to make help
			i = sb.indexOf(" ", i) + 1;						//find the index to insert help
			help = sb.charAt(indHelp) + "ay";               // make help  
			sb.insert(sb.indexOf(" ", i) , help);			// insert help
			sb.deleteCharAt(i);								// delete first char			
			itr = sb.indexOf(" ", itr+1);
		}
		
		indHelp = sb.indexOf(" ",indHelp) + 1;	
		help = sb.charAt(indHelp) + "ay";
		sb.insert(sb.length() , help);		
		sb.deleteCharAt(sb.lastIndexOf(" ") + 1);		
		
		return sb;
	}
	
	
	
}
